Skip to main content

Average of a Single Matrix along Columns

This is an example of computing the average of a single matrix input along the columns of the matrix.

from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np

class ExampleSingleMatrixAlong0(Model):
    def define(self):        n = 3        m = 6
        # Declare a matrix of shape 3x6 as input        M1 = self.declare_variable(            'M1',            val=np.arange(n * m).reshape((n, m)),        )
        # Output the axiswise average of matrix M1 along the columns        self.register_output(            'single_matrix_average_along_0',            csdl.average(M1, axes=(0, )),        )

sim = Simulator(ExampleSingleMatrixAlong0())sim.run()
print('M1', sim['M1'].shape)print(sim['M1'])print('single_matrix_average_along_0',      sim['single_matrix_average_along_0'].shape)print(sim['single_matrix_average_along_0'])
0.0002968311309814453 s=========================================M1 (3, 6)[[ 0.  1.  2.  3.  4.  5.] [ 6.  7.  8.  9. 10. 11.] [12. 13. 14. 15. 16. 17.]]single_matrix_average_along_0 (6,)[ 6.  7.  8.  9. 10. 11.]